package ev;

import java.util.Random;

import TeamPursuit.MensTeamPursuit;
import TeamPursuit.SimulationResult;

public class EA_Team_Men21 implements Team_Men {

	@Override
	public void emulation() {
		boolean [] strategy = {true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false};
	    int [] power = {550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550};
    	SimulationResult result = null;
        MensTeamPursuit mensTeamPursuit = new MensTeamPursuit();
        
        int times = 5000;
        double finalTime = 1000;
        int index, pValue;
        Random r = new Random();
        boolean b;
		while(times>0){			
			b=r.nextBoolean();
			//System.out.println("b= "+b);
			if(b){		 
			 
				index = r.nextInt(power.length);
				pValue = power[index];
				power[index] = r.nextInt(1001)+200;
				//System.out.println(index+" "+power[index]);
				try {
					result = mensTeamPursuit.simulate(strategy, power);
				} catch (Exception e) {
					e.printStackTrace();
				}
				if(result.getProportionCompleted() < 0.9999998){
					//System.out.println(result.toString());
					power[index] = pValue;
					continue;
				}
				if(result.getFinishTime()<finalTime){
					finalTime = result.getFinishTime();
					//System.out.println(finalTime);
				}
				else{
					power[index] = pValue;
					//System.out.println(finalTime);
				}
			}else{
			index = r.nextInt(strategy.length);
			boolean strategyValue = strategy[index];
			strategy[index] = !strategyValue;
			//System.out.println(index+" "+power[index]);
	       	try {
	       		result = mensTeamPursuit.simulate(strategy, power);
				} catch (Exception e) {
					e.printStackTrace();
				}
				if(result.getProportionCompleted() < 0.9999998){
					//System.out.println(result.toString());
					strategy[index] = strategyValue;
					continue;
				}
				if(result.getFinishTime()<finalTime){
					finalTime = result.getFinishTime();
					//System.out.println(finalTime);
				}else{
					strategy[index] = strategyValue;
					//System.out.println(finalTime);
				}
			}
			times--;
		}

		System.out.println(finalTime);
    	try {
    		result = mensTeamPursuit.simulate(strategy, power);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(result.toString());
		for(int i =0; i < strategy.length; i++){
			System.out.print(strategy[i]+" ");
		}
		System.out.println();
		for(int i =0; i < power.length; i++){
			System.out.print(power[i]+" ");
		}
	}

}